package com.toggery.POS.controller;

import com.alibaba.fastjson.JSONObject;
import com.toggery.POS.service.LoginService;
import com.toggery.POS.tool.Loginbean;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台登录
 */
@RestController
@RequestMapping(value = "/login")
@Api(tags = "登录操作")
public class LoginController {
    @Autowired
    LoginService service = new LoginService();

    @RequestMapping(value="/validate", method= RequestMethod.POST )
    public Map loginValidate(Model model,@RequestBody(required=false) Map map1){
        if(map1==null){
            return null;
        }
        else {
            String username = map1.get("username").toString();
            String password = map1.get("password").toString();
            Map map = new HashMap() ;

//        if(username==null||password==null){
//            model.addAttribute("msg","用户名或密码输入错误");
//            return "/welcome";
//        }

            if (service.loginValidate(username, password)) {
                map.put("result", true);
                map.put("msg", "正在进入主页");
            } else {
                map.put("result", false);
                map.put("msg", "用户名或密码输入错误");
            }
            return map;
        }
    }
    @RequestMapping("/logout")
    public String logout(){
        //注销后

        return "/login";
    }
}
